import { defineStore } from 'pinia'

export const useUserStore = defineStore('user', {
  state: () => ({
    userId: localStorage.getItem('userId') || null,
    username: localStorage.getItem('username') || null,
    token: localStorage.getItem('token') || null
  }),

  actions: {
    setUser(userData) {
      this.userId = userData.user_id
      this.username = userData.username
      this.token = userData.token
      
      // 保存到本地存储
      localStorage.setItem('userId', userData.user_id)
      localStorage.setItem('username', userData.username)
      localStorage.setItem('token', userData.token)
    },

    clearUser() {
      this.userId = null
      this.username = null
      this.token = null
      
      // 清除本地存储
      localStorage.removeItem('userId')
      localStorage.removeItem('username')
      localStorage.removeItem('token')
    }
  }
}) 